package com.compass.mobile.ui.activity

import UserInfoManager
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import androidx.lifecycle.lifecycleScope
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.ActivityCaseHistoryBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.constant.BASE_PDF
import com.compass.mobile.framework.constant.BASE_PDF_FULL
import com.compass.mobile.framework.constant.CASE_ADD
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.CaseHistory
import com.compass.mobile.presenters.CaseHistoryPresenter
import com.compass.mobile.ui.dialog.CodeDialog
import com.compass.mobile.ui.viewModel.InquiryViewModel
import com.compass.mobile.ui.viewModel.PersonalCenterViewModel
import com.compass.mobile.widget.MyBridgeAdapter
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.net.URLDecoder

/**
 * Description： 病历记录
 * Date:2024/6/19 11:08
 */
class CaseHistoryRecordActivity : BaseVBActivity<ActivityCaseHistoryBinding>() {

    private val personalCenterViewModel: PersonalCenterViewModel by viewModels()
    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    private var caseHistoryList: MutableList<CaseHistory> = mutableListOf()
    private var mPage = 1
    private var isHaveNext = true
    private var type: String = ""

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, CaseHistoryRecordActivity::class.java)
            context.startActivity(intent)
        }
    }


    override fun initView(savedInstanceState: Bundle?) {
        specialColumn()
    }

    override fun initData() {
        personalCenterViewModel.caseHistory(UserInfoManager.getPatientInfo()?.id, mPage, 10)
        personalCenterViewModel.caseHistoryLiveData.observe(this) {
            dismissLoading()
            if (mPage == 1) {
                if (it.isNullOrEmpty()) {
                    isHaveNext(0)
                    mBinding?.emptyView?.visible()
                    mBinding?.llBelow?.gone()
                } else {
                    isHaveNext(it.size)
                    mBinding?.emptyView?.gone()
                    caseHistoryList = it
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, caseHistoryList)
                }
            } else {
                if (it.isNullOrEmpty()) {
                    isHaveNext(0)
                    mPage-- //恢复上一次的page
                    showToast("没有下一页了")
                } else {
                    isHaveNext(it.size)
                    caseHistoryList = it
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, caseHistoryList)
                }
            }
        }
    }

    private fun isHaveNext(number: Int) {
        if (number < 5) {
            isHaveNext = false
        } else {
            mBinding?.llBelow?.gone()
            isHaveNext = true
        }
    }

    override fun intListener() {
        mBinding?.tvPrevious?.onClick {
            if (mPage > 1) {
                mPage--

            } else {
                showToast("没有上一页了")
            }
        }

        mBinding?.tvNextPage?.onClick {
            if (isHaveNext) {
                mPage++

            } else {
                showToast("没有下一页了")
            }
        }
    }

    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            val item = caseHistoryList[position]
            openPDF(item.signPdf)
        }
    }
    private fun openPDF(pdfUrl:String?){
//        WebDetailActivity.start(
//           this,
//            BASE_PDF_FULL + URLDecoder.decode(pdfUrl, "utf-8"),""
//        )

       val oderCodeDialog =
            CodeDialog.Builder(this@CaseHistoryRecordActivity, BASE_PDF_FULL + URLDecoder.decode(pdfUrl, "utf-8"))
        oderCodeDialog.show()

    }

    @SuppressLint("RestrictedApi")
    fun specialColumn() {
        mBinding?.caseItem?.setNumColumns(2)
        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)

        arrayObjectAdapter = ArrayObjectAdapter(CaseHistoryPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            this,
            arrayObjectAdapter, onItemClickListener
        )

        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, caseHistoryList)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )

        lifecycleScope.launch {
            delay(500)
            //延迟获取焦点
            mBinding?.caseItem?.requestFocus()
        }
    }

}
